package com.cjack.frenchexcel.controller;

import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.config.CommonConfig;
import com.cjack.frenchexcel.config.LayPage;
import com.cjack.frenchexcel.model.ConsumerRecordConditionModel;
import com.cjack.frenchexcel.model.ConsumerRecordModel;
import com.cjack.frenchexcel.service.ConsumerRecordService;
import com.cjack.frenchexcel.service.GoodsService;
import com.cjack.frenchexcel.table.ConsumerRecordTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 发票管理
 */
@RestController
@RequestMapping("/invoice")
public class InvoiceEndpoint extends BaseEndpoint{

    @Autowired
    ConsumerRecordService service;
    @Autowired
    GoodsService goodsService;

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult list( HttpSession session, LayPage layPage, ConsumerRecordConditionModel condition) {

        try{
            condition.setInvoice( "---");//传一个非空值即可，组装sql的时候，组装成invoice非空
            Object loginUserId = session.getAttribute( CommonConfig.SESSION_NAME);
            condition.setLoginUserId( (Long)loginUserId);
            Page< ConsumerRecordTable> resultPage = service.list( layPage, condition);
            layPage.setData( resultPage.getContent());
            layPage.setCount( Long.valueOf( resultPage.getTotalElements()).intValue());
            return AjaxResult.SUCCESS( layPage);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    @RequestMapping(value = "/uploadInvoice", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult uploadInvoice( HttpSession session, ConsumerRecordModel model) {
        try{
            Object loginUserId = session.getAttribute( CommonConfig.SESSION_NAME);
            model.setInvoiceUploadUserId( (Long)loginUserId);
            model.setUpdateTime( new Date());
            service.uploadInvoice( model);
            return AjaxResult.SUCCESS();
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 审核
     * @return
     */
    @RequestMapping(value = "/reviewInvoice", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult reviewInvoice( ConsumerRecordModel model) {
        try{
            String customHouse = model.getCustomHouse();
            model.setUpdateTime( new Date());
            model.setInvoiceReviewTime( new Date());
            model.setInvoiceMoney( model.getInvoiceMoney());
            service.add( model);
            model = service.getModel( model.getId());
            model.setCustomHouse( customHouse);
            //生成商品 -将海关码保存到新生成的商品表
            if( model.getInvoiceStatus().equals( "PASS")){
                goodsService.initGoods( model);
            }else{//重置关联云盘的状态
                service.resetCloudDisk( model);
                //发票审核拒绝的策略
                service.reviewInvoiceReject( model);
            }
            return AjaxResult.SUCCESS( "审核成功");
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

}
